//
//  XMMsgToolsView.m
//  XunMi
//
//  Created by MacBook on 2024/4/2.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMMsgToolsView.h"
#import "XMMsgToolsCell.h"

@interface XMMsgToolsView () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray <XMMsgToolsModel *> *dataArray;
@end

@implementation XMMsgToolsView

- (void)awakeFromNib {
    [super awakeFromNib];
    self.dataArray = [NSMutableArray array];
    NSArray <NSString *> *arr = @[@"亲密度",@"关注",@"守护",@"赞"];
    
    [arr enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XMMsgToolsModel *model = [[XMMsgToolsModel alloc] init];
        model.type = idx + 1;
        model.imgName = kStringFormat(@"msg_tool%ld", idx + 1);
        model.title = obj;
        [self.dataArray addObject:model];
    }];

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake((kScreenWidth - 48 - 26 * 3) / 4, 72);
    flowLayout.minimumLineSpacing = 26;
    flowLayout.minimumInteritemSpacing = 26;
    [self.collectionView setCollectionViewLayout:flowLayout];
    
    [self.collectionView registerNib:kNibWithName(kStringFromClass(XMMsgToolsCell)) forCellWithReuseIdentifier:kStringFromClass(XMMsgToolsCell)];
}

- (void)reloadData:(NSInteger)unread {
    if (unread > 0) {
        XMMsgToolsModel *model = [self.dataArray firstObject];
        model.showRed = YES;
        [self.collectionView reloadData];
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XMMsgToolsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kStringFromClass(XMMsgToolsCell) forIndexPath:indexPath];
    XMMsgToolsModel *model = [self.dataArray safe_objectAtIndex:indexPath.item];
    cell.model = model;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.toolsBlock) {
        self.toolsBlock([self.dataArray safe_objectAtIndex:indexPath.item]);
    }
}

@end


@implementation XMMsgToolsModel


@end
